package ${package.ServiceImpl};


import ${cfg.packageName}.service.I${entity}Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import ${cfg.packageName}.mapper.${entity}Mapper;
import ${cfg.packageName}.model.bean.${entity}ListReq;
import ${cfg.packageName}.model.bean.${entity}ListForPageReq;
import ${cfg.packageName}.model.vo.${entity}VO;
import ${cfg.packageName}.model.bo.${entity}BO;
import ${cfg.packageName}.model.dto.${entity}DTO;
import ${cfg.packageName}.model.dto.${entity}SaveDTO;
import ${cfg.packageName}.model.bean.${entity}SaveReq;
import ${cfg.packageName}.model.bean.${entity}DelReq;
import ${cfg.packageName}.model.entity.${entity};
import com.mti.framework.core.util.SnowFlake;
import com.mti.digital.security.DataValidUtils;
import com.mti.framework.core.dao.IEntityDao;
import com.github.pagehelper.page.PageMethod;
import com.mti.framework.core.util.BeanTools;
import com.mti.framework.core.util.SnowFlake;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.mti.framework.core.bean.PageableOut;

/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} implements I${entity}Service {

    @Autowired
    private ${entity}Mapper ${table.entityPath}Mapper;

    @Autowired
    private IEntityDao entityDao;


    @Override
    public List<${entity}VO> get${entity}List(${entity}ListReq ${table.entityPath}ListReq) {
        ${entity}DTO ${table.entityPath}DTO = BeanTools.copy(${entity}DTO.class, ${table.entityPath}ListReq);
        List<${entity}> list =  ${table.entityPath}Mapper.get${entity}List(${table.entityPath}DTO);
        return BeanTools.copy(${entity}VO.class, list);
    }

    @Override
    public PageableOut<${entity}VO> get${entity}ListPage(${entity}ListForPageReq ${table.entityPath}ListForPageReq) {
        Integer currPage = ${table.entityPath}ListForPageReq.getCurrPage();
        Integer pageSize = ${table.entityPath}ListForPageReq.getPageSize();
        Page<?> page = PageMethod.startPage(currPage, pageSize);
        ${entity}DTO ${table.entityPath}DTO = BeanTools.copy(${entity}DTO.class, ${table.entityPath}ListForPageReq);
        List<${entity}> list =  ${table.entityPath}Mapper.get${entity}List(${table.entityPath}DTO);
        return new PageableOut(BeanTools.copy(${entity}VO.class, list), page.getTotal(), pageSize, currPage);
    }

    @Override
    @Transactional
    public boolean saveOrUpdate${entity}(${entity}SaveDTO ${table.entityPath}SaveDTO){
        ${entity} ${table.entityPath} = BeanTools.copy(${entity}.class, ${table.entityPath}SaveDTO);
        boolean flag ;
        <#list table.fields as field>
        <#if field.keyFlag>
        if (StringUtils.isBlank(${table.entityPath}SaveDTO.get${field.capitalName}())) {
            // String.valueOf(IdUtil.createSnowflake(1, 1).nextId())
            ${table.entityPath}.set${field.capitalName}(SnowFlake.nextStringId());
        </#if>
        </#list>
            flag = entityDao.insert(${table.entityPath});
        } else {
            flag = entityDao.updateById(${table.entityPath});
        }
        return flag;
    }


    @Override
    @Transactional
    public boolean delete${entity}ById(${entity}DelReq ${table.entityPath}DelReq){
    <#list table.fields as field>
        <#if field.keyFlag>
        return ${table.entityPath}Mapper.delete${entity}ById(${table.entityPath}DelReq.get${field.capitalName?cap_first}())>0;
        </#if>
    </#list>
    }

    @Override
    public ${entity}VO get${entity}(${entity}DTO ${table.entityPath}DTO){
        <#list table.fields as field>
            <#if field.keyFlag>
                ${entity}BO ${table.entityPath}BO = ${table.entityPath}Mapper.select${entity}ById(${table.entityPath}DTO.get${field.capitalName?cap_first}());
            </#if>
        </#list>
        return BeanTools.copy(${entity}VO.class, ${table.entityPath}BO);
    }

    @Override
    public int batchSave${entity}(List<${entity}SaveDTO> ${table.entityPath}SaveDTOs){
        //List<${entity}> ${table.entityPath}s =  BeanTools.copy(${entity}.class, ${table.entityPath}SaveDTOs);
        //return entityDao.insertBatch(${table.entityPath}s);
        DataValidUtils.isEmptyCollection(${table.entityPath}SaveDTOs,"批量保存列表不能为空");
        ${table.entityPath}SaveDTOs.stream().forEach(i->i.setId(SnowFlake.nextStringId()));
        return ${table.entityPath}Mapper.insertBatch${entity}(${table.entityPath}SaveDTOs);
    }

}
</#if>